package lol.clann.minecraft.springboot.plugin.command;

import lol.clann.minecraft.springboot.api.bukkit.command.TabComplete;
import lol.clann.minecraft.springboot.api.annotation.command.Command;
import lol.clann.minecraft.springboot.api.annotation.command.CommandDefine;
import lol.clann.minecraft.springboot.api.annotation.command.Sender;
import lol.clann.minecraft.springboot.api.bukkit.utils.InventoryUtils;
import lol.clann.minecraft.springboot.api.bukkit.utils.ItemStackUtils;
import lol.clann.minecraft.springboot.api.bukkit.utils.PluginUtils;
import lol.clann.minecraft.springboot.api.model.Slot;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 单个/多个物品保存成文件,或者从文件读取
 *
 * @author pyz
 * @date 2019/5/6 11:57 PM
 */
@CommandDefine(name = "springboot", mapping = "item")
public class ItemStackSaver {

    @Autowired
    private PluginUtils pluginUtils;
    @Autowired
    private ItemStackUtils itemStackUtils;
    @Autowired
    private InventoryUtils inventoryUtils;

    @Command(showArgs = "item save <key>", onlyPlayer = true, des = "用指定key保存手上的物品")
    private boolean save(@Sender Player sender, String key) throws IOException {
        ItemStack item = sender.getItemInHand();
        if (itemStackUtils.isEmpty(item)) {
            sender.sendMessage("请把物品拿在手上");
            return false;
        }
        itemStackUtils.save(item, key);
        sender.sendMessage(ChatColor.GREEN + "保存成功");
        return true;
    }

    @Command(showArgs = "item get <key>", onlyPlayer = true, des = "获取指定物品", tabComplete = GetItemStackTabComplete.class)
    private boolean get(@Sender Player sender, String key) throws IOException {
        ItemStack item = itemStackUtils.get(key);
        HashMap<Integer, ItemStack> rejects = sender.getInventory().addItem(item);
        sender.sendMessage(ChatColor.GREEN + "读取成功");
        if (!rejects.isEmpty()) {
            sender.sendMessage(ChatColor.RED + "部分物品读取失败");
        }
        return true;
    }

    @Command(showArgs = "item saveAll <key>", onlyPlayer = true, des = "用指定的key保存背包全部物品")
    private boolean saveAll(@Sender Player sender, String key) throws IOException {
        List<ItemStack> is = new ArrayList<>();
        for (Slot slot : inventoryUtils.allItems(sender.getInventory())) {
            is.add(slot.getItem());
        }
        if (is.isEmpty()) {
            sender.sendMessage("您背包为空");
            return false;
        }
        itemStackUtils.saves(is, key);
        sender.sendMessage(ChatColor.GREEN + "保存成功");
        return true;
    }

    @Command(showArgs = "item getAll <key>", onlyPlayer = true, des = "获取指定物品集", tabComplete = GetItemStacksTabComplete.class)
    private boolean getAll(@Sender Player sender, String key) throws IOException {
        List<ItemStack> items = itemStackUtils.gets(key);
        HashMap<Integer, ItemStack> rejects = sender.getInventory().addItem(items.toArray(new ItemStack[items.size()]));
        ;
        sender.sendMessage(ChatColor.GREEN + "读取成功");
        if (!rejects.isEmpty()) {
            sender.sendMessage(ChatColor.RED + "" + rejects.size() + "个物品读取失败");
        }
        return true;
    }

    @Component
    public static class GetItemStackTabComplete implements TabComplete {
        @Autowired
        private ItemStackUtils itemStackUtils;

        @Override
        public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
            String prefix = args[args.length - 1].toLowerCase();
            File[] files = itemStackUtils.getItemStackDir().listFiles();
            List<String> candidates = new ArrayList<>();
            for (File file : files) {
                if (!file.isFile()) {
                    continue;
                }
                if (file.getName().toLowerCase().startsWith(prefix)) {
                    candidates.add(file.getName());
                }
            }
            return candidates;
        }
    }

    @Component
    public static class GetItemStacksTabComplete implements TabComplete {
        @Autowired
        private ItemStackUtils itemStackUtils;

        @Override
        public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
            String prefix = args[args.length - 1].toLowerCase();
            File[] files = itemStackUtils.getItemStacksDir().listFiles();
            List<String> candidates = new ArrayList<>();
            for (File file : files) {
                if (!file.isFile()) {
                    continue;
                }
                if (file.getName().toLowerCase().startsWith(prefix)) {
                    candidates.add(file.getName());
                }
            }
            return candidates;
        }
    }

}
